<template>
    <div class="title">
        添加商品
    </div>
    <el-form
        :model="formData"
        :rules="rules"
        label-width="auto"
        style="max-width: 1000px"
        ref="formRef"
        :hide-required-asterisk="true"
    >
        <el-form-item label="商品名称" prop="name">
            <el-input v-model="formData.name" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="商品分类" prop="category" style="width: 250px">
            <el-select
                placeholder="请选择商品分类"
                v-model="formData.category"
            >
                <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
            <el-input-number v-model="num" :min="1" @change="handleChange" :step="1"/>
        </el-form-item>
        <el-form-item label="商品图片" prop="img">
            <el-upload
                class="avatar-uploader"
                action="http://8.137.157.16:9002/goods/goods_img_upload"
                :show-file-list="false"
                :on-success="handleUploadSuccess"
                :on-error="handleUploadError"
            >
                <img v-if="formData.img" :src="formData.img" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
        </el-form-item>
        <el-form-item label="商品描述" prop="desc">
            <el-input v-model="formData.desc" type="textarea" style="width: 100%;" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm">添加商品</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { Plus } from '@element-plus/icons-vue';
import { serveUrl } from '../../utils/common';
import axios from 'axios';
import { addGoods } from '../../api/account';

// 创建axios实例
const request = axios.create({
    baseURL: serveUrl,
    timeout: 10000
});

const num = ref(1);
const handleChange = (value) => {
    console.log(value);
};
const rules = {
    name: [
        { required: true, message: '请输入商品名称', trigger: 'blur' }
    ],
    category: [
        { required: true, message: '请选择商品分类', trigger: 'change' }
    ],
    desc: [
        { required: true, message: '请输入商品描述', trigger: 'blur' }
    ],
    img: [
        { required: true, message: '请上传商品图片', trigger: 'change' }
    ]
};
const formRef = ref(null);
const formData = ref({
    name: '',
    category: '',
    price: '',
    desc: '',
    img: ''
});
const options = [
    { value: '热销榜', label: '热销榜' },
    { value: '收藏门店超值专享', label: '收藏门店超值专享' },
    { value: '新品专享', label: '新品专享' },
    { value: '鲜炒盖饭', label: '鲜炒盖饭' },
    { value: '炒饭系列', label: '炒饭系列' },
    { value: '千万销量', label: '千万销量' },
    { value: '营养适配', label: '营养适配' },
    { value: '满减专区', label: '满减专区' },
    { value: '撸烤串', label: '撸烤串' },
    { value: '(冒菜)套餐自选区', label: '(冒菜)套餐自选区' },
    { value: '小吃搭饮料', label: '小吃搭饮料' }
];

const handleUploadSuccess = (response, uploadFile) => {
    if (response.code === 0) {
        formData.value.img = serveUrl + response.imgUrl;
        ElMessage.success('上传成功');
    } else {
        ElMessage.error('上传失败');
    }
};

const handleUploadError = (error, uploadFile) => {
    ElMessage.error('上传失败');
};


const submitForm = async () => {
    await formRef.value.validate();
    try {
        // 从完整图片地址中提取图片名字
        const imgUrl = formData.value.img.split('/').pop();
        const response = await addGoods({
            name: formData.value.name,
            category: formData.value.category,
            price: num.value.toString(),
            imgUrl: imgUrl,
            goodsDesc: formData.value.desc
        });
        console.log(response);
        
        if (response.code === 0) {
            ElMessage.success(response.msg);
        } else {
            ElMessage.error(response.msg);
        }
    } catch (error) {
        ElMessage.error('添加商品失败，请稍后重试');
    }
};
</script>

<style lang="scss" scoped>
.title {
    width: 100%;
    height: 50px;
    font-size: 20px;
    color: #333;
    line-height: 50px;
    padding-left: 20px;
    text-align: left;
    border-bottom: 1px solid #dcdfe6;
}

.avatar-uploader.avatar {
    width: 180px;
    height: 180px;
    display: block;
    border: 1px dashed var(--el-border-color);
}

.avatar-uploader.el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader.el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
}

.avatar-uploader {
    width: 180px;
    height: 180px;
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
    text-align: center;
    line-height: 180px;
    font-size: 28px;
    color: #8c939d;
}

.el-form {
    padding: 20px;
}

.el-button {
    margin-left: 100px;
}
</style>